#!/bin/bash
COMMAND=0
SCRIPT=$0
INSTALLPATH=/usr/local/advtty
DEAMON=advttyd
SERVICE=vcomd
DRIVER=advvcom
CONFIGFILE=$INSTALLPATH/advttyd.conf
DEAMONFILE=$INSTALLPATH/$DEAMON
DKMSFILE=/lib/modules/`uname -r`/updates/dkms/$DRIVER.ko
KERNEL=`uname -r|cut -c1-3`
if [ $KERNEL = "2.4" ] 
then
DRIVERFILE=$INSTALLPATH/$DRIVER.o
else
DRIVERFILE=$INSTALLPATH/$DRIVER.ko
fi

echo $DRIVERFILE

RETURN=
OBJECTIVE=

hasdriver()
{
	COUNT=`lsmod | grep $DRIVER -c`
	if [ $COUNT -gt 0 ]
	then
		echo "kernel moduel $DRIVER.ko detected..."
		RETURN=1
	else
		echo "kernel moduel $DRIVER.ko not installed... try objective \"insert\" first"
		RETURN=0
	fi
}

insdriver()
{
	if [ $KERNEL = "2.4" ] 
	then
	echo "inserting kernel moduel $DRIVER.o..."
	insmod $DRIVERFILE
	elif [ -e $DKMSFILE ]
	then
	echo "inserting DKMS module $DRIVER.ko..."
	insmod $DKMSFILE
	else
	echo "inserting kernel moduel $DRIVER.ko..."
	insmod $DRIVERFILE
	fi
}

rmdriver()
{
	if [ $KERNEL = "2.4" ] 
	then
	echo "removing kernel moduel $DRIVER..."
	rmmod $DRIVER
	else
	echo "removing kernel moduel $DRIVER.ko..."
	rmmod $DRIVER.ko
	fi
}

stopService()
{
	echo "stoping all local services..."
	killall -s 9 $SERVICE
}

startDeamon()
{
	# for 2.4.x kernel, we should make node by myself
	if [ $KERNEL = "2.4" ] 
	then
		i=1
		while [[ $i -le 256 ]]
		do
			if [ -z $(awk '$2=="vttyAP"{print $1}' /proc/devices) ]
			then
				mknod /dev/vttyAP$i c $(awk '$2=="vttyap/%d"{print $1}' /proc/devices) $i
			else			
				mknod /dev/vttyAP$i c $(awk '$2=="vttyAP"{print $1}' /proc/devices) $i
			fi			
			((i = i + 1))
		done
	fi
	echo "invoking local deamon..."
	install -m 777 -d /tmp/advmon
	#`$DEAMONFILE`
	$DEAMONFILE

}

stopDeamon()
{
#	COUNT=`ps -A|grep $DEAMON -c`
#	while [ $COUNT -gt 0 ]
#	do
#		PID=`ps -A|grep $DEAMON |sed -r s/^[[:space:]]*//g|sed  -r s/"[[:space:]]+.+[[:space:]]+[[:digit:]]+:[[:digit:]]+:[[:digit:]]+[[:space:]]+$DEAMON"/""/g | sed '/^[ \t]*$/d'|head -n 1`
#
#		echo "killing $DEAMON $PID"
#
#		kill -9 $PID
#		COUNT=`ps -A|grep $DEAMON -c`
#	done
	killall -s 9 $DEAMON
	if [ $KERNEL = "2.4" ] 
	then
        	rm -rf /dev/vttyAP*
	fi
}


usage()
{
cat << EOF
usage: $0 options

This script lists helps you manage local advtty service.

OPTIONS:
   -h      Show this message
   -o	   Execute objective:
   		start 	- start advtty local service
		stop 	- stop advtty local service
		restart	- restart advtty local service
		insert	- insert $DRIVER.ko module
		remove	- remove $DRIVER.ko module

Example:
	$0 -o start
	$0 -o restart
	$0 -o stop

EOF
}

while getopts “ho:” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         o)
             OBJECTIVE=$OPTARG
             ;;
         ?)
	     usage
             exit
             ;;
     esac
done

case $OBJECTIVE in
	start)
		hasdriver
		if [ $RETURN -eq 1 ]
		then
			echo "starting service...."
			startDeamon
		else
			echo "can not start service..."
			insdriver
			startDeamon
		fi
		exit 1
	;;
	restart)
		hasdriver
		if [ $RETURN -eq 1 ]
		then
			echo "starting service...."
			startDeamon
		else
			echo "can not start service..."
			insdriver
			startDeamon
		fi
		exit 1

	;;
	stop)
		echo stop
		stopService
		stopDeamon
		exit 1
	;;
	insert)
		insdriver
		exit 1
	;;
	remove)
		stopService
		stopDeamon
		rmdriver
		exit 1
	;;
esac
usage


